package com.masiyi.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @Author masiyi
 * @Date 2023/12/18
 * @PackageName:com.masiyi.servlet
 * @ClassName: MyTheadLocal
 * @Description: TODO
 * @Version 1.0
 */
@WebServlet(urlPatterns = "/myThreadLocal")
public class MyThreadLocal extends HttpServlet {

    ThreadLocal threadLocal = new ThreadLocal<String>();
    /**
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(threadLocal.get());
        threadLocal.set("你好");
        method1();
    }

    private void method1() {
        System.out.println(threadLocal.get());
        method2();
    }

    private void method2() {
        threadLocal.remove();
        System.out.println(threadLocal.get());
    }

    /**
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(threadLocal.get());
        super.doPost(req, resp);
    }
}
